<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 
*/
class CourseCard extends MY_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('Courses/CourseCardModel','card');
	}

	//列表页
	public function index()
	{
		$data['course_id'] = intval($this->uri->segment(3));
		$data['card_list'] = $this->card->get_list_data($data['course_id']);

		$this->load->view('coures_card_list', $data);
	}


	//添加、编辑页
	public function coures_card_info()
	{
		$data['course_id'] = intval($this->uri->segment(3));

		$data['card_id'] = intval($this->uri->segment(4));

		//编辑
		if ($data['card_id'] > 0) 
		{
			$res = $this->card->get_card_row($data['card_id']);
		}

		$data['card_name'] = isset($res['card_name']) ? $res['card_name'] : '';
		$data['price'] = isset($res['price']) ? $res['price'] : '';
		$data['course_num'] = isset($res['course_num']) ? $res['course_num'] : '';
		$data['brief'] = isset($res['brief']) ? $res['brief'] : '';

		$this->load->view('coures_card_info',$data);
	}


	//保存入库
	public function insert_add_card()
	{
		$field = array('card_name','price','course_num','brief','course_id');
		$arr = $this->input->post($field, TRUE);

		$card_id = $this->input->post('card_id');

		//添加
		if (empty($card_id)) 
		{
			$res = $this->card->inser_card_data($arr);

			if ($res) 
			{
				$data['status'] = 200;
				$data['message'] = '添加成功！';

			}
			else
			{
				$data['status'] = 400;
				$data['message'] = '添加失败！';
			}
		}
		//更新
		else
		{
			$res = $this->card->updata_card_data($arr,$card_id);

			if ($res) 
			{
				$data['status'] = 200;
				$data['message'] = '更新成功！';

			}
			else
			{
				$data['status'] = 400;
				$data['message'] = '更新失败！';
			}

		}

		echo json_encode($data);exit();
		
	}


	//删除数据
	public function delete_card()
	{
		$card_id = $this->input->get('card_id', true);

		$res = $this->card->remove_card($card_id);

		if ($res){
			$data['status'] = 200;
			$data['message'] = '删除成功！';
		}
		else{
			$data['status'] = 400;
			$data['message'] = '删除失败！';
		}

		echo json_encode($data);exit();

	}

	
	//获取列表数据
	public function get_list(){
		
		$list = $this->card->get_all_data();
		
		$this->data_out($list);
	}

}



?>